<template>
  <div class="pagination">
    <button v-show="syntax.start > 1">上一页</button>
    <button v-show="syntax.start > 1">1</button>
    <button v-show="syntax.start >= 2">···</button>
    <button
      v-for="(item, index) in syntax.end"
      :key="index"
      v-show="item >= syntax.start && item <= syntax.end"
      @click="sendIndex(item)"
    >
      {{ item }}
    </button>
    <button v-show="showPage - syntax.end > 1">···</button>
    <button v-show="showPage - syntax.end >= 1">{{ showPage }}</button>
    <button v-show="showPage - syntax.end > 2">下一页</button>

    <button style="margin-left: 30px">共 {{ pagetotal }} 条</button>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
export default {
  name: "Pagination",
  data() {
    return {};
  },
  props: ["pagesize", "pagetotal", "pagenum", "showtotal"],
  //showtotal 分页器显示个数

  methods: {
    // 子传父 把当前点击值传回
    sendIndex(index){
      this.$emit('accept',index)

    }
  },
  computed: {
    //计算可显示的页面数
    showPage() {
      let { pagetotal, pagesize } = this;
      let showPage = Math.floor(pagetotal / pagesize);
      return showPage;
    },

    // 计算开始的页码与结束页码
    syntax() {
      // 假设 当前所在页面在第5页
     
      let { pagetotal, pagesize, pagenum, showtotal,showPage } = this;
      let start , end ;
     
        let naviLeft = pagenum - Math.floor(showtotal/2)

        let naviRight = pagenum + Math.floor(showtotal/2)
         start = naviLeft
         end = naviRight
      //不正常情况
      //设置的页码 > 可显示页数
      if (showtotal >= showPage) {
        //页码小于显示页码数
        start = 1;
        end = showPage;
      //判断左边

      }else{
        //正常情况下,左右边不在中间的时候
      //   获取分页格的前后

        if(naviLeft <= 0){
          start = 1
          end = showtotal
        }
        if(naviRight >= showPage){
          end = showPage
          start = showPage - showtotal + 1
        }
          
        
      }
      return {start , end}
    }
  },
  mounted() {},
  
  }



</script>

<style lang="less" scoped>
.pagination {
  button {
    margin: 0 5px;
    background-color: #f4f4f5;
    color: #606266;
    outline: none;
    border-radius: 2px;
    padding: 0 4px;
    vertical-align: top;
    display: inline-block;
    font-size: 13px;
    min-width: 35.5px;
    height: 28px;
    line-height: 28px;
    cursor: pointer;
    box-sizing: border-box;
    text-align: center;
    border: 0;

    &[disabled] {
      color: #c0c4cc;
      cursor: not-allowed;
    }

    &.active {
      cursor: not-allowed;
      background-color: #409eff;
      color: #fff;
    }
  }
}
</style>
